import axios, {AxiosResponse, InternalAxiosRequestConfig} from 'axios'
import pinia from '../store/pinia'
import {ElLoading} from 'element-plus'
import { userStore } from '../store/user'
import { useServerStore } from '../store/server'
const store = userStore(pinia)
const serverStore = useServerStore(pinia)

// 动态获取baseURL的函数
const getDynamicBaseUrl = (type: 'main' | 'admin' | 'websocket') => {
    return serverStore.getApiBaseUrl(type)
}

const baseUrl1:string = "http://megasensing.cn:4000/" // 外网数据
// 使用动态端口配置，如果store中没有配置则回退到env配置
const getBaseUrl2 = () => getDynamicBaseUrl('main') || import.meta.env.VITE_APP_BASEURL_MAIN
const getBaseUrl3 = () => getDynamicBaseUrl('admin') || import.meta.env.VITE_APP_BASEURL_ADMIN

// 本地串口接口
const instace = axios.create({
    baseURL: getBaseUrl2(),
    timeout: 80000
})
let loadingInstance:any = null
let newLoading:any = null
// 配置请求拦截器
instace.interceptors.request.use((config:InternalAxiosRequestConfig) => {
    loadingInstance = ElLoading.service({
         background: "rgba(255,255,255,.1)",
        text: "加载中请稍等"
    })
    return config
})

// 配置响应拦截器
instace.interceptors.response.use((config:AxiosResponse) => {
    loadingInstance.close()
    return config
},(err:any) => {
    loadingInstance.close()
    console.log(err)
})

// 本地串口接口
const adminInstance = axios.create({
    baseURL: getBaseUrl3(),
    timeout: 20 * 1000
})

// 配置请求拦截器
adminInstance.interceptors.request.use((config:InternalAxiosRequestConfig) => {
    return config
})

// 配置响应拦截器
adminInstance.interceptors.response.use((config:AxiosResponse) => {
    return config
},(err:any) => {
    console.log(err)
})

const adminInstanceLoading = axios.create({
    baseURL: getBaseUrl3(),
    timeout: 20 * 1000
})
adminInstanceLoading.interceptors.request.use((config:InternalAxiosRequestConfig) => { 
    newLoading = ElLoading.service({
        background: "rgba(255,255,255,.1)",
        text: "加载中请稍等"
    })
    return config
})

adminInstanceLoading.interceptors.response.use((res:AxiosResponse) => {
    newLoading.close()
    return res
},(err:any) => {
    newLoading.close()
    console.log(err)
})

// 下载excel数据的响应
const downInstance = axios.create({
    baseURL: getBaseUrl3(),
    timeout: 100000000000000000
})

let loadingInstance1:any = null

downInstance.interceptors.request.use((config) => { 
    store.initNum = 0
    let timers = Math.floor(store.total / 35890)
    let speed = Math.floor(100 / timers)
    loadingInstance1 = setInterval(() => {
        console.log(timers)
        if (timers > 0) {
            timers --
            store.initNum += speed
        } else {
            console.log("要结束了")
            clearInterval(loadingInstance1)
        }
    },1000)
    console.log(timers)
    return config
})

downInstance.interceptors.response.use((config) => {
    store.initNum = 100
    clearInterval(loadingInstance1)
    return config
})
// let loadingInstance:any = null



// 外网的接口
const networkInstace = axios.create({
    baseURL: baseUrl1,
    timeout: 8000
})

// let loadingInstance:any = null
// 配置请求拦截器
networkInstace.interceptors.request.use((config:InternalAxiosRequestConfig) => {
    // loadingInstance = ElLoading.service({
    //     background: 'rgba(0,0,0,.3)'
    // })
    return config
})

// 配置响应拦截器
networkInstace.interceptors.response.use((config:AxiosResponse) => {
    // loadingInstance.close()
    return config
})


// 更新所有axios实例的baseURL
const updateAxiosBaseUrls = () => {
    instace.defaults.baseURL = getBaseUrl2()
    adminInstance.defaults.baseURL = getBaseUrl3()
    adminInstanceLoading.defaults.baseURL = getBaseUrl3()
    downInstance.defaults.baseURL = getBaseUrl3()
    console.log('Axios实例baseURL已更新:', {
        main: getBaseUrl2(),
        admin: getBaseUrl3()
    })
}

export default {instace,networkInstace,downInstance,adminInstance,adminInstanceLoading,updateAxiosBaseUrls}